<script setup lang="ts">
import { consultOrderId, getConsultOrderPre, getPatientInfo } from '@/apis/consult';
import type { ConsultOrderPreData, PartialConsult } from '@/types/consult';
import { onMounted, ref } from 'vue';
import { useConsultStore } from '@/stores'
import type { PatientDataType } from '@/types/user';
import { showConfirmDialog, showToast } from 'vant';
import { onBeforeRouteLeave } from 'vue-router';
import router from '@/router';
const store = useConsultStore()
//定义问诊预支付订单数据
const orderPre = ref<ConsultOrderPreData>()
//定义患者详情信息
const patientInfo = ref<PatientDataType>()
//定义问诊预支付订单函数
const consultOrderPre = async () => {
  const res = await getConsultOrderPre({
    type: store.consult.type,
    illnessType: store.consult.illnessType
  })
  orderPre.value = res.data
  //设置优惠券
  store.setCouponId(orderPre.value.couponId)
}
//定义患者详情信息函数
const patientInfoRes = async () => {
  if (!store.consult.patientId) return
  const res = await getPatientInfo(store.consult.patientId)
  patientInfo.value = res.data
}

type Key = keyof PartialConsult
onMounted(() => {
  //刷新页面，数据会丢失，关闭后提示
  const vaildArr: Key[] = ['type', 'consultFlag', 'depId', 'illnessDesc', 'illnessTime', 'illnessType', 'patientId']
  const isVaild = vaildArr.every(item => store.consult[item] !== undefined)
  if (!isVaild) return showConfirmDialog({
    title: '温馨提示',
    message: '问诊信息不完整请重新填写，如有未支付的问诊订单可在问诊记录中继续支付！',
    closeOnPopstate: false
  }).then(() => {
    router.push('/')
  })
  consultOrderPre()
  patientInfoRes()
})
//同意勾选
const isAgree = ref(false)
//动作面板
const show = ref(false)
const loading = ref(false)
//保存问诊订单id
const orderId = ref<string>('')
const onsubmit = async () => {
  if (!isAgree.value) return showToast('请勾选已同意协议')
  loading.value = true
  const res = await consultOrderId(store.consult)
  orderId.value = res.data.id
  loading.value = false
  store.clear()
  show.value = true
}
//生成订单后阻止返回
onBeforeRouteLeave(() => {
  if (orderId.value) return false
})

//生成订单后不可关闭支付抽屉
const onClose = () => {
  showConfirmDialog({
    title: '关闭支付',
    message: '取消支付将无法获得医生回复，医生接诊名额有限，是否确认关闭？',
    confirmButtonText: '继续支付',
    cancelButtonText: '仍要关闭'
  }).then(() => {
    return false
  }).catch(() => {
    orderId.value = ''
    //让其跳转到问诊记录
    router.push('/user/consult')
    //关闭动作面板
    return true
  })
}

</script>

<template>
  <div class="consult-pay-page" v-if="orderPre && patientInfo">
    <cp-nav-bar title="支付" />
    <div class="pay-info">
      <p class="tit">图文问诊 {{ orderPre?.payment }} 元</p>
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <p class="desc">
        <span>极速问诊</span>
        <span>自动分配医生</span>
      </p>
    </div>
    <van-cell-group>
      <van-cell title="优惠券" :value="`-¥${orderPre.couponDeduction}`" />
      <van-cell title="积分抵扣" :value="`-¥${orderPre.pointDeduction}`" />
      <van-cell title="实付款" :value="`¥${orderPre.actualPayment}`" class="pay-price" />
    </van-cell-group>
    <div class="pay-space"></div>
    <van-cell-group>
      <van-cell title="患者信息"
        :value="`${patientInfo.name} | ${patientInfo.genderValue} | ${patientInfo.age}岁`"></van-cell>
      <van-cell title="病情描述" :label="store.consult.illnessDesc"></van-cell>
    </van-cell-group>
    <div class="pay-schema">
      <van-checkbox v-model="isAgree">我已同意 <span class="text">支付协议</span></van-checkbox>
    </div>
    <van-submit-bar :loading="loading" button-type="primary" :price="orderPre.actualPayment * 100" button-text="立即支付"
      text-align="left" @click="onsubmit" />
    <!-- 动作面板 -->
    <cp-pay-sheet v-model:show="show" :order-id="orderId" :actual-payment="orderPre.actualPayment" pay-callback="/room"
      :onClose="onClose"></cp-pay-sheet>
  </div>
  <!-- 骨架屏 -->
  <div class="consult-pay-page" v-else>
    <cp-nav-bar title="支付" />
    <van-skeleton title :row="3" style="margin-top: 15px;" />
  </div>
</template>

<style lang="scss" scoped>
.consult-pay-page {
  padding: 46px 0 50px 0;
}

.pay-info {
  display: flex;
  padding: 15px;
  flex-wrap: wrap;
  align-items: center;

  .tit {
    width: 100%;
    font-size: 16px;
    margin-bottom: 10px;
  }

  .img {
    margin-right: 10px;
    width: 38px;
    height: 38px;
    border-radius: 4px;
    overflow: hidden;
  }

  .desc {
    flex: 1;

    >span {
      display: block;
      color: var(--cp-tag);

      &:first-child {
        font-size: 16px;
        color: var(--cp-text2);
      }
    }
  }
}

.pay-price {
  ::v-deep() {
    .vam-cell__title {
      font-size: 16px;
    }

    .van-cell__value {
      font-size: 16px;
      color: var(--cp-price);
    }
  }
}

.pay-space {
  height: 12px;
  background-color: var(--cp-bg);
}

.pay-schema {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;

  .text {
    color: var(--cp-primary);
  }
}

::v-deep() {
  .van-submit-bar__button {
    font-weight: normal;
    width: 160px;
  }
}
</style>